package main

import (
	"JoRedis/config"
	"JoRedis/lib/logger"
	"JoRedis/tcp"
	"JoRedis/util"
	"fmt"
	"os"
)

var banner = `
     ____.     __________           .___.__        
    |    | ____\______   \ ____   __| _/|__| ______
    |    |/  _ \|       _// __ \ / __ | |  |/  ___/
/\__|    (  <_> )    |   \  ___// /_/ | |  |\___ \ 
\________|\____/|____|_  /\___  >____ | |__/____  >
                       \/     \/     \/         \/  
`

func main() {
	print(banner)
	configFileName := os.Getenv("CONFIG")
	if configFileName == "" {
		if util.ExistFile("redis.conf") {
			configFileName = "redis.conf"
			config.ReadConfig(configFileName)
		} else {
			config.DefaultConfig()
		}
	}

	logSetPtr := &logger.Setting{
		Path:       "logs",
		Name:       "JoRedis",
		Ext:        "log",
		TimeFormat: "2006-01-02",
	}
	// 日志配置
	logger.Config(logSetPtr)

	serverConfigPtr := &tcp.ServerConfig{
		NetAddr: fmt.Sprintf("%s:%d", config.PropPtr.Bind, config.PropPtr.Port),
	}

	logger.Info("服务器监听:", config.PropPtr.Self)

	err := tcp.ListenAndServer(serverConfigPtr, tcp.MakeRedisHandler())
	if err != nil {
		logger.Error(err)
	}
}
